package com.yq.bdm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yq.bdm.controller.common.AbstractController;
import com.yq.bdm.entity.RoleInfoEntity;
import com.yq.bdm.service.RoleInfoService;
import com.yq.bdm.utils.PageUtils;
import com.yq.bdm.utils.ResultMap;

/**
 * 角色表
 * 
 * @author yuq
 * @date 2017-07-21 16:35:14
 */
@Controller
@RequestMapping("/roleinfo")
public class RoleInfoController extends AbstractController {

	@Autowired
	private RoleInfoService roleInfoService;

	@RequestMapping("/index.html")
	public String list() {
		return "admin/roleinfo/roleinfo.html";
	}

	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	public ResultMap list(String id, String roleId, String parentRoleId,
			String roleName, String description, String gmtCreate,
			String gmtModified) {

		Map<String, Object> map = new HashMap<>();
		// 查询条件
		map.put("id", id);
		map.put("roleId", roleId);
		map.put("parentRoleId", parentRoleId);
		map.put("roleName", roleName);
		map.put("description", description);
		map.put("gmtCreate", gmtCreate);
		map.put("gmtModified", gmtModified);

		// 获取分页参数，添加到查询请求参数中
		getPage(map);

		// 查询列表数据
		List<RoleInfoEntity> roleInfoList = roleInfoService.queryList(map);
		int total = roleInfoService.queryTotal(map);
		PageUtils pageUtil = new PageUtils(roleInfoList, total, map);
		return ResultMap.ok().put("page", pageUtil);
	}
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/treeList")
	public ResultMap treeList() {

		// 查询列表数据
		List<RoleInfoEntity> roleInfoList = roleInfoService
				.queryList(null);
		return ResultMap.ok().put("list", roleInfoList);
	}

	/**
	 * 跳转到新增页面
	 */
	@RequestMapping("/goAdd")
	public String goAdd() {
		return "admin/roleinfo/add.html";
	}

	/**
	 * 跳转到编辑页面
	 */
	@RequestMapping("/goEdit/{id}")
	public String goEdit(@PathVariable("id") Long id) {
		// 设置当前control的访问url，edit页面需要使用
		request.setAttribute("namespaceUrl",
				request.getContextPath() + "/roleinfo");
		RoleInfoEntity roleInfo = roleInfoService.queryObject(id);
		request.setAttribute("roleInfo", roleInfo);
		return "admin/roleinfo/edit.html";
	}

	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{roleId}")
	public ResultMap info(@PathVariable("roleId") Long roleId) {
		RoleInfoEntity roleInfo = roleInfoService.queryObjectByRoleId(roleId);

		return ResultMap.ok().put("roleInfo", roleInfo);
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	public ResultMap save(@RequestBody RoleInfoEntity roleInfo) {
		roleInfoService.save(roleInfo);

		return ResultMap.ok();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public ResultMap update(@RequestBody RoleInfoEntity roleInfo) {
		roleInfoService.update(roleInfo);

		return ResultMap.ok();
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public ResultMap delete(@RequestBody Long[] ids) {
		roleInfoService.deleteBatch(ids);

		return ResultMap.ok();
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/deleteByRoleId")
	public ResultMap deleteByRoleId(@RequestBody Long[] roleIds) {
		roleInfoService.deleteBatchByRoleId(roleIds);

		return ResultMap.ok();
	}

	/**
	 * 删除成功后，查询列表的数据
	 */
	@ResponseBody
	@RequestMapping("/deleteAfterList")
	public ResultMap deleteAfterList(@RequestBody Long[] ids) {
		roleInfoService.deleteBatch(ids);

		Map<String, Object> map = new HashMap<>();
		// 获取分页参数，添加到查询请求参数中
		getPage(map);
		// 查询列表数据
		List<RoleInfoEntity> roleInfoList = roleInfoService.queryList(map);
		int total = roleInfoService.queryTotal(map);
		PageUtils pageUtil = new PageUtils(roleInfoList, total, map);
		return ResultMap.ok().put("page", pageUtil);
	}
}
